import Vue from 'vue'
import Vuex from 'vuex'

import { createPersistedState, createSharedMutations } from 'vuex-electron'

import modules from './modules'
// import imageModule from './image';
// import user from './modules/user';
import user from './user';
import { DownloadItem } from 'electron';
import { stat } from 'original-fs';
Vue.use(Vuex)

export default new Vuex.Store({
  
  // modules: {
  //   // 注册你的 Vuex store 模块
  //   image: imageModule
  // },
  modules: {
    user
  },
  plugins: [
    createPersistedState(),
    createSharedMutations()
  ],

  strict: process.env.NODE_ENV !== 'production',
  state: {
    favoriteImages: [], // 存储收藏的图片数组
    DownloadImages: []
  },
  mutations: {
    addToFavorites(state, image) {
      state.favoriteImages.push(image);
    },
    addToDownload(state,image){
      state.DownloadImages.push(image)
    },
    removeFromFavorites(state, image) {
      const index = state.favoriteImages.findIndex(item => item.id === image.id);
      if (index !== -1) {
        state.favoriteImages.splice(index, 1);
      }
    },
    SET_USER(state, payload) {
      state.user = payload.username;
    },
    clearData(state) {
      // 重置状态为初始值或空值
      state.favoriteImages = [];
      // state.list = [];
      // ... 其他需要清空的数据
    },
    clearDownload(state){
      state.DownloadImages = [];
      console.log('老子还进来了')
    },
  
  },
  getters: {
    getFavoriteImages(state) {
      return state.favoriteImages;
    },
    getDownloadImages(state){
      return state.DownloadImages;
    }
  
},
actions : {
  addToFavorites({ commit }, image) {
    commit('addToFavorites', image);
  },
  addToDownload({commit},image){
    commit('addToDownload',image);
  },
  // other actions...
  loginAction({ commit }, payload) {
    return new Promise((resolve, reject) => {
      // 执行异步操作，例如发起登录请求
      // 完成后调用 resolve 方法返回结果

      // 示例：假设在这里发起登录请求
      axios.post('/api/login', payload)
        .then(response => {
          // 登录成功，调用 resolve 方法返回结果
          resolve(response.data);
        })
        .catch(error => {
          // 登录失败，调用 reject 方法返回错误信息
          reject(error);
        });
    });
  },
  clearDownload({commit}){
    commit('clearDownload')
  }
}


})
